﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using UI.Core;
using System.Reflection;
using BO;
using UIControls;
namespace UI
{
    public partial class AppMainForm : FormBase
    {
       
       
        private DockContent FindDocument(string text)
        {
            ApplicationContext ap = new ApplicationContext();
            if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
            {
                foreach (Form form in MdiChildren)
                {
                    if (form.Text == text)
                    {
                        return form as DockContent;
                    }
                }

                return null;
            }
            else
            {
                foreach (DockContent content in dockPanel.Documents)
                {
                    if (content.DockHandler.TabText == text)
                    {
                        return content;
                    }
                }

                return null;
            }
        }



        public void CloseAllDocuments()
        {
            if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
            {
                foreach (Form form in MdiChildren)
                {
                    form.Close();
                }
            }
            else
            {
                IDockContent[] documents = dockPanel.DocumentsToArray();
                foreach (IDockContent content in documents)
                {
                    content.DockHandler.Close();
                }
            }
        }

        protected override void OnClosed(EventArgs e)
        {
            CloseAllDocuments();
            base.OnClosed(e);
            Application.Exit();
        }
        const int MAX_HEIGHT = 100;
        const int MIN_HEIGHT = 25;
        public AppMainForm()
        {
            InitializeComponent();
            this.Text = string.Format("北京影响力-{0}",GlobalVar.User.UserName);
            if (!Utility.IsDesignMode())
            {
                this.IsMdiContainer = true;
                dockPanel.DocumentStyle = DocumentStyle.DockingMdi;
                dockPanel.RightToLeftLayout = true;

                this.WindowState = FormWindowState.Maximized;

                dockPanel.TabStop = false;

                this.Load += new EventHandler(AppMainForm_Load);
                BuildMainMenu();
                LoadAllMenu();
            }

          
        }
        private CsMenuStrip myMenuStrip;
        private CsToolStripMenuItem editMenu;
        private CsToolStripMenuItem reportMenu;
        private CsToolStripMenuItem dxMenu;
        private CsToolStripMenuItem toolMenu;
        private CsToolStripMenuItem securityMenu;
        void BuildMainMenu()
        {
            this.myMenuStrip = new CsMenuStrip();
            this.myMenuStrip.BackColor = GlobalUIVar.gBackColor;
            
            if (GlobalVar.User.HasNoRole)//权限
                myMenuStrip.Visible = false;
            this.editMenu = new CsToolStripMenuItem();
          
            this.reportMenu = new CsToolStripMenuItem();
            this.toolMenu = new CsToolStripMenuItem();
            this.securityMenu = new CsToolStripMenuItem();
            this.dxMenu = new CsToolStripMenuItem();
            this.myMenuStrip.SuspendLayout();
            //
            //fileMenu
            //
            securityMenu.Text = "权限管理";
            
            
            if (!GlobalVar.User.IsSystemUser)//权限
                securityMenu.Visible = false;

            //
            //dxMenu
            //
            this.dxMenu.Name = "dxMenu";
            this.dxMenu.Size = new System.Drawing.Size(54, 25);
            this.dxMenu.BackColor = GlobalUIVar.gBackColor;
            this.dxMenu.Text = "短信报表";
           
             // 
            // myMenuStrip
            // 
            this.myMenuStrip.Font = new System.Drawing.Font("Microsoft YaHei", 12F);
            this.myMenuStrip.GripStyle = System.Windows.Forms.ToolStripGripStyle.Visible;
            this.myMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
                this.securityMenu,
            this.editMenu,
            this.reportMenu,
            this.dxMenu,
            this.toolMenu});
            this.myMenuStrip.Location = new System.Drawing.Point(0, 0);
            this.myMenuStrip.Name = "myMenuStrip";
            this.myMenuStrip.Size = new System.Drawing.Size(882, 29);
            this.myMenuStrip.TabIndex = 0;
            this.myMenuStrip.Text = "menuStrip1";
            // 
            // editMenu
            // 
            this.editMenu.Name = "editMenu";
            this.editMenu.Size = new System.Drawing.Size(54, 25);
            this.editMenu.Text = "编辑";
            // 
            // reportMenu
            // 
            this.reportMenu.Name = "reportMenu";
            this.reportMenu.Size = new System.Drawing.Size(54, 25);
            this.reportMenu.Text = "报表";
            // 
            // toolMenu
            // 
            this.toolMenu.Name = "toolMenu";
            this.toolMenu.Size = new System.Drawing.Size(54, 25);
            this.toolMenu.Text = "工具";


            this.myMenuStrip.ResumeLayout(false);
            this.myMenuStrip.PerformLayout();
            this.Controls.Add(this.myMenuStrip);
        }
        void AppMainForm_Load(object sender, EventArgs e)
        {
           
        }
        Assembly asm = Assembly.Load(BO.GlobalVar.UIAssembly);
        void LoadAllMenu()
        {

            try
            {
            
                foreach (var t in asm.GetTypes())
                {
                    if (t.IsInterface || t.IsAbstract || t.IsNested)
                        continue;
                    if (t.GetInterface("IMenuForm") == null)
                        continue;
                    CsToolStripMenuItem item = null;
                    if (t.Namespace=="UI.Security")
                    {
                        item = CreateItemByType(t);
                        if (item == null)
                            continue;
                        item.BackColor = GlobalUIVar.gBackColor;
                        this.securityMenu.DropDownItems.Add(item);                       
                        continue;
                    }
                    switch (t.BaseType.Name)
                    {

                        case "EditFormBase":
                            item = CreateItemByType(t);
                            if (item == null)
                                continue;
                            this.editMenu.DropDownItems.Add(item);
                            break;
                        case "ReportFormBase":
                            item = CreateItemByType(t);
                            if (item == null)
                                continue;
                            this.reportMenu.DropDownItems.Add(item);
                            break;
                        case "ToolFormBase":
                            item = CreateItemByType(t);
                            if (item == null)
                                continue;
                            this.toolMenu.DropDownItems.Add(item);
                            break;
                        case "TextFormBase":
                            item = CreateItemByType(t);
                            if (item == null)
                                continue;
                            this.dxMenu.DropDownItems.Add(item);
                            break;
                    }
                    if (item != null)
                        item.BackColor = GlobalUIVar.gBackColor;

                }

            }
            catch (ExceptionEx exp)
            {
                ShowChildForm("TestForm", typeof(DBAForm));
            }
        }
        CsToolStripMenuItem CreateItemByType(Type t)
        {
            if (t.FullName.IndexOf('+') > 0)
                return null;
            var ac =GlobalVar.User.ReadRoleAccessList.FirstOrDefault(a => a.FormName == t.FullName && a.ControlName == t.FullName);
         

            dynamic obj = asm.CreateInstance(t.FullName);

            var item = new CsToolStripMenuItem();

            if (ac != null)
            {
                item.Enabled = !ac.Enabled;
                item.Visible = !ac.Visible;
            }
            
            item.Text = obj.Text;
            item.Tag = t;
            item.Click += new EventHandler(item_Click);
            return item;
        }

        private void item_Click(object sender, EventArgs e)
        {

            var item = sender as ToolStripMenuItem;
            var t = item.Tag as Type;
        
            ShowChildForm(item.Text, t);
        }

        void ShowChildForm(string caption, Type formType)
        {
            try
            {

                this.Cursor = Cursors.WaitCursor;
                DockContent frm = FindDocument(caption);
                if (frm == null)
                {
                    frm = asm.CreateInstance(formType.FullName) as DockContent;
                }
               
                frm.Show(this.dockPanel);
                frm.BringToFront();

            }
            catch (ExceptionEx exp)
            {
                ExcptionHandler.ShowExcption(exp);
            }
            finally
            {

                this.Cursor = Cursors.Default;
            }
        }
    


    }
}
